package com.ruoyi.project.men.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.men.domain.MenNeedUserDO;
import com.ruoyi.project.men.service.MenNeedUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户领取需求Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/men/menNeedUser")
public class MenNeedUserController extends BaseController
{
	@Autowired
	private MenNeedUserService menNeedUserService;

	/**
	 * 查询用户领取需求列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menNeedUser:list')")
	@GetMapping("/list")
	public TableDataInfo list(MenNeedUserDO menNeedUser)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menNeedUser);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		List<MenNeedUserDO> list = menNeedUserService.list(stringObjectMap);
		return getDataTable(list);
	}

	/**
	 * 导出用户领取需求列表
	 */
	@PreAuthorize("@ss.hasPermi('men:menNeedUser:export')")
	@Log(title = "用户领取需求", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MenNeedUserDO menNeedUser)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menNeedUser);
		List<MenNeedUserDO> list = menNeedUserService.list(stringObjectMap);
		ExcelUtil<MenNeedUserDO> util = new ExcelUtil<MenNeedUserDO>(MenNeedUserDO.class);
		return util.exportExcel(list, "menNeedUser");
	}

	/**
	 * 获取用户领取需求详细信息
	 */
	@PreAuthorize("@ss.hasPermi('men:menNeedUser:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(menNeedUserService.get(id));
	}

	/**
	 * 获取领取需求的用户
	 */
	@PreAuthorize("@ss.hasPermi('men:menNeedUser:list')")
	@GetMapping(value = "/needId/{id}")
	public TableDataInfo getInfos(@PathVariable("id") String id)
	{

		Map<String, Object> map = new HashMap<>();
		map.put("delFlag","0");
		map.put("needId",id);
		List<Map<String, Object>> list = menNeedUserService.userList(map);
		return getDataTable(list);
	}

	/**
	 * 新增用户领取需求
	 */
	@PreAuthorize("@ss.hasPermi('men:menNeedUser:add')")
	@Log(title = "用户领取需求", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MenNeedUserDO menNeedUser)
	{
		menNeedUser.setId(IdUtil.simpleUUID());
		menNeedUser.setDelFlag(0);
		menNeedUser.setCaAt(new Date());
		return toAjax(menNeedUserService.save(menNeedUser));
	}

	/**
	 * 修改用户领取需求
	 */
	@PreAuthorize("@ss.hasPermi('men:menNeedUser:edit')")
	@Log(title = "用户领取需求", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MenNeedUserDO menNeedUser)
	{
		menNeedUser.setUpAt(new Date());
		return toAjax(menNeedUserService.update(menNeedUser));
	}

	/**
	 * 删除用户领取需求
	 */
	@PreAuthorize("@ss.hasPermi('men:menNeedUser:remove')")
	@Log(title = "用户领取需求", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenNeedUserDO menNeedUser = menNeedUserService.get(string);
				menNeedUser.setDelFlag(1);
				menNeedUser.setUpAt(new Date());
				menNeedUserService.update(menNeedUser);
			}
		}
		return toAjax(ids.length);
		//return toAjax(menNeedUserService.deleteMenNeedUserByIds(ids));
	}
}
